@echo off
title 修复服务器启动问题
color 0C

echo.
echo ╔══════════════════════════════════════════════════════════════╗
echo ║                  修复服务器启动问题                          ║
echo ╚══════════════════════════════════════════════════════════════╝
echo.

echo 🔧 正在修复服务器启动问题...

echo.
echo 1️⃣ 清理PM2进程...
pm2 delete all >nul 2>&1

echo.
echo 2️⃣ 检查并安装依赖...
cd /d "%~dp0backend"
if not exist node_modules (
    echo 正在安装依赖...
    npm install
)

echo.
echo 3️⃣ 启动服务器...
pm2 start server.js --name "neighborhood-help-backend"

echo.
echo 4️⃣ 保存配置...
pm2 save

echo.
echo 5️⃣ 测试服务器...
timeout /t 3 /nobreak >nul
curl -s http://192.168.1.6:3002/api/health >nul 2>&1
if errorlevel 1 (
    echo ❌ 服务器启动失败，请检查错误日志
    pm2 logs neighborhood-help-backend --lines 10
) else (
    echo ✅ 服务器启动成功！
)

echo.
echo 📊 当前状态:
pm2 status

echo.
echo 🌐 服务器地址: http://192.168.1.6:3002/api
echo 📱 现在可以重新编译小程序了！

echo.
pause
